﻿using System.Configuration;
using YawetagDemo.Console.Services;
using YawetagDemo.Framework.Client.Services;
using YawetagDemo.Framework.Impl.Data;
using YawetagDemo.Framework.Impl.Services;
using YawetagLib.System.Data;
using YawetagLib.System.Data.L2Sql;
using YawetagLib.System.Facade.Services;

namespace YawetagDemo.Console.Core
{
    class Program
    {
        static void Main()
        {
            DoInProcessWork();
            DoInterProcessWork();
        }

        private static void DoInProcessWork()
        {
            System.Console.WriteLine("******** IN-PROCESS WORK ********");
            System.Console.WriteLine();

            string connectionString = ConfigurationManager.ConnectionStrings["default-connection-string"].ConnectionString;

            DataContextBuilder.RegisterDataContexts(DataContextInfo.New(
                typeof(DbModelDataContext), i => new DataContextAdapter(new DbModelDataContext(connectionString))));

            using (IFacadeFactory facadeFactory = FacadeFactoryBuilder.GetFactory())
            {
                facadeFactory.RegisterService<CoreService>();
                facadeFactory.RegisterService<MusicService>();

                DoWork(facadeFactory);
            }

            DataContextBuilder.UnRegisterFactory();
        }

        private static void DoInterProcessWork()
        {
            System.Console.WriteLine("******** INTER-PROCESS WORK ********");
            System.Console.WriteLine();

            using (IFacadeFactory facadeFactory = FacadeFactoryBuilder.GetFactory())
            {
                facadeFactory.RegisterService<WCFCoreServiceProxy>();
                facadeFactory.RegisterService<WCFMusicServiceProxy>();

                DoWork(facadeFactory);
            }
        }

        private static void DoWork(IFacadeFactory facadeFactory)
        {
            Facade facade = facadeFactory.Get<Facade>();

            facade.DoBasicRetrieval();
            facade.DoCallbackOperations();
            facade.DoComplexRetrieval();
            facade.DoCRUDOperations();
        }
    }
}
